<template>
	<div class="box">
		<div class="box1">
			<h2>当前求和为：{{sum}}</h2>
			<h4>组件简介：{{description}}</h4>
		</div>
		<div class="box2">
			<h4>当前求和放大10倍后：{{bigSum}}</h4>
			<h4>简短的简介：{{shortDescription}}</h4>
		</div>
		<select v-model.number="n">
			<option value="1">1</option>
			<option value="2">2</option>
			<option value="3">3</option>
		</select>
		<button @click="increment(n)">+</button>
		<button @click="decrement(n)">-</button>
		<button @click="jiaOdd(n)">当前求和为奇数再+</button>
		<button @click="jiaWait(n)">等一等再+</button>
		<button @click="jiaServer">问问服务器到底+几？</button>
	</div>
</template>

<script>
	import {mapState,mapGetters,mapMutations,mapActions} from 'vuex'

	export default {
		name:'Count',
		data() {
			return {
				n:1
			}
		},
		computed: {
			...mapState(['sum','description']),
			...mapGetters(['bigSum','shortDescription'])
		},
		methods: {
			//第一种写法（对象写法）靠mapMutations生成两个可以直接和mutations对话的两个函数
			...mapMutations({increment:'JIA',decrement:'JIAN'}),
			
			//靠程序员亲自去写：incrementIfOdd、incrementWait、incrementServer去联系actions —— 麻烦
			/* incrementIfOdd(){
				this.$store.dispatch('jiaOdd',this.n)
			},
			incrementWait(){
				this.$store.dispatch('jiaWait',this.n)
			},
			incrementServer(){
				this.$store.dispatch('jiaServer')
			} */

			//第一种写法（对象写法）
			/* ...mapActions({
				incrementIfOdd:'jiaOdd',
				incrementWait:'jiaWait',
				incrementServer:'jiaServer',
			}) */

			//第二种写法（数组写法）
			...mapActions(['jiaOdd','jiaWait','jiaServer'])

		},	
		mounted() {
			console.log(this)
		},
	}
</script>

<style>
	*{
		margin-left: 10px;
	}
	.box{
		background-color: pink;
		padding: 20px;
	}
	.box1{
		background-color: skyblue;
		padding: 20px;
		margin-bottom: 10px;
	}
	.box2{
		background-color: orange;
		padding: 20px;
	}
</style>